{oapdf_gg}
Office software is closely linked to the PDF,the PDF is also must have to control!      Set home Page  Add to Favorites  
You are here:Home > PDF Application > PDF Development
PDF Development
PHP in the preparation of PDF document generator method tutorial
Finishing by: Date:2009-04-17 21:09:08 Popularity: Tags:

PHP is one of the greatest advantages of new technologies to support its very easy language scalability enables developers to easily add new modules, and technical organizations around the world support and the many expansion modules support makes PHP has become the most complete one of the Web programming language. Expansion modules currently available have been able to allow developers to implement IMAP and POP3 operations, can be dynamically generated images and Shockwave Flash animation, for credit card authentication, encryption and decryption of sensitive data, but also be able to resolve XML format. But this is not all, now, there is a new module to bind with PHP, it is PDFLib expansion module, which allows developers to dynamically generate PDF (Adobe Portable Document Format) file format, on the first look at the following look at how to use the PHP module.
In order to be able to operate so that PHP has the ability to PDF format documents, you must first install your system to expand PDFLib library, if you are using a Lunix system, you can http://www.pdflib.com/pdflib/index.html to download a compiler and, if you are using a Windows system, it's even easier, just need to download PDFLib a compiled database, and then in the PHP configuration file to the corresponding line of the Notes can be removed.

extension = php_pdf.dll

If it is the dynamic loading, it could be a reference to the following order:

dl ( "php_pdf.dll ");

In addition, you must have an Adobe Acrobat PDF Reader to browse PDF format, if you do not, you can http://www.adobe.com/ free download.
Once you do a good job of preparation, you can create PDF documents, and the following is a simple example:

//Create a new PDF document handle
$ pdf = PDF_new ();

//open a file
PDF_open_file ($ pdf, "PDFTest.pdf ");

//start a new page (A4)
PDF_begin_page ($ pdf, 595, 842);

//get and use the font object
$ arial = PDF_findfont ($ pdf, "Arial", "host", 1);
PDF_setfont ($ pdf, $ arial, 10);

//output text
PDF_show_xy ($ pdf, "This is an exam of PDF Documents, It is a good Lib,", 50, 750);
PDF_show_xy ($ pdf, "If you like, please try yourself!", 50, 730);

//end of page
PDF_end_page ($ pdf);

//Close and save the file
PDF_close ($ pdf);
?>

and then save into a PHP document, carried out in the browser browser, PHP would be the implementation of the above code, it generates a new PDF document and save it to the designated location.
Now we analyze what kind of code, to use PHP to create PDF documents, there are four steps: 1, handle the creation of the document; 2, the registration document fonts and colors; 3, the function provided by PDFLib file handles to write text or drawing; 4, save the document.
First of all, create a PDF document handle, the following syntax:

$ pdf = PDF_new ();

this task is PDF_new () function to complete, it returns a PDF document handle, the handle will be the follow-up of all the action.
Next step is to the PDF file a name, by the PDF_open_file () function to complete, it needs to handle files created previously, and custom parameters so the file name:

PDF_open_file ($ pdf, "PDFTest.pdf ");

Once we have created a document, you can use PDF_begin_page () function to insert a new page in which the:

PDF_begin_page ($ pdf, 595, 842);

then PDF_end_page () the end of the page.
Note here that in PDF_begin_page () function, there is the other two parameters, they represent the page size width and height, unit is pounds (point, 1 pounds equal to 1/72 inch), perhaps not in your strengths Mathematics , PHP also provides the majority of the standard page size, such as A4, the above example is the use of A4 size.
In the call PDF_begin_page () function and PDF_end_page () function between the code is to write the contents of a PDF document, the content can be text, images, etc., as well as geometry. Example of just writing a line of text, a character first, and then wrote the text in the document. Through PDF_findfont () and PDF_setfont () function selection and registration of fonts is very convenient, PDF_findfont () function to prepare a document to use the fonts, required parameters are the name of the font, use the codes, whether or not to embed fonts PDF file. PDF_findfont () function returns a font object, it will be PDF_setfont () function to use.

$ arial = PDF_findfont ($ pdf, "Arial", "host", 1);
PDF_setfont ($ pdf, $ arial, 10);

Once we set a font, you can use PDF_show_xy () function to specify the location of the page to write in the string.

PDF_show_xy ($ pdf, "This is an exam of PDF Documents, It is a good Lib,", 50, 750);
PDF_show_xy ($ pdf, "If you like, please try yourself!", 50, 730);

PDF_show_xy () function used to write the contents of the page, the last two parameters is to write the coordinates of the location of the string, pay attention to the origin coordinates (0,0) is in the lower left corner of the document. Once the text has been finished, the page will be closed PDF_end_page (), of course, you can write more pages. After all the pages finished with PDF_close () function to close the document, this time back to save the document to call PDF_open_file () function when the specified file name and path, then handle the destruction of documents.
PDFLib library can do much more than these things, you can add images in the page, the document in front of us as an example, add in the text below an image file, the following statement to add images to achieve the function:

$ image = PDF_open_image_file ($ pdf, "jpeg", "PDFImageTest.jpg");
PDF_place_image ($ pdf, $ image, 50, 650, 0.25);

is not very simple? PDF_open_image_file () function to open a graphics file, you can accept image types: GIF, JPEG, TIFF and PNG, the function returns image handle, PDF_place_image () function handles the use of images in front of the image inserted into the PDF document. Attention to the coordinates of the location here is the image of the lower-left corner, the last parameter is the image displayed when the scale factor, 1 is the same size and the actual display, 0.5 is half the size of the original show.

there isArticle InformationsComment Information
Category column

Site Help | Site Map | Feedback |
OAPDF.COM Copyright 2009 V1.1